package com.shuqi.model.sharedprefs;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;

/**
 * 请求服务器静态不需要重试的相关域名
 * @author yhw
 */
public class StaticDomains {
    
    /**静态域名：广告/精品的CDN域名*/
    private final String DEFAULT_STATICDOMAIN_AD = "r17.11222.cn";
    
    private static StaticDomains staticDomains;
    
    /** 广告和推广墙的CDN域名 */
    public static final int TYPE_ID_STATICDOMAIN_AD = 1;
    
    private StaticDomains(){}
    public static StaticDomains getInstance(){
        if(staticDomains==null){
            staticDomains = new StaticDomains();
        }
        return staticDomains;
    }
    
    /***
     * 保存静态的域名：不需要重试的CDN域名
     * @param webUrList 数据格式: 1|r17.11222.cn 保存格式: staticDomain_1 --> r17.11222.cn
     * @param context
     */
    public boolean saveStaticDomains(List<String> staticDomainList, Context context) {
        SharedPreferences sp =
                context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_STATICDOMAINS,
                        Context.MODE_PRIVATE);
        int num = 0;
        Editor editor = sp.edit();
        if (staticDomainList != null && staticDomainList.size() > 0) {
            for (int i = 0; i < staticDomainList.size(); i++) {
                String str = staticDomainList.get(i).trim();
                String sKey = str.substring(0, str.indexOf("|"));
                String sValue = str.substring(str.indexOf("|") + 1);
                editor.putString("staticDomain_" + sKey, sValue);
                num++;
            }
        }
        boolean result = editor.commit(); 
        Log4an.e("StaticDomains", "staticDomain 数据：保存"+(result?"成功":"失败")+"，条数："+num);
        return result;
    }
    
    /***
     * 获取静态不需重试的域名
     * @param context
     * @param webUrlType:从1开始 StaticDomains.TYPE_ID_STATICDOMAIN_***
     */
    public String getStaticDomainKey(Context context, int staticDomainType) {
        SharedPreferences sp =
                context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_STATICDOMAINS,
                        Context.MODE_PRIVATE);
        String domain = sp.getString("staticDomain_" + staticDomainType, DEFAULT_STATICDOMAIN_AD);
        Log4an.e("StaticDomains", "staticDomain 数据：读取："+domain);
        return domain;
    }

}
